#include "utils.hpp"
#include "logger.hpp"

#include <chrono>
#include <ctime>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <sstream>

std::string utils::time_get()
{
    // 获取当前时间
    auto now = std::chrono::system_clock::now();
    auto in_time_t = std::chrono::system_clock::to_time_t(now);

    // 转换为本地时间
    struct tm buf;
    localtime_r(&in_time_t, &buf);

    // 使用 stringstream 格式化时间
    std::stringstream ss;
    ss << std::put_time(&buf, "%Y-%m-%d-%H-%M-%S");
    return ss.str();
}

bool utils::folder_create(const std::string& path, bool recursive)
{
    try {
        if (recursive) {
            return std::filesystem::create_directories(path);
        } else {
            if (!std::filesystem::exists(path)) {
                std::filesystem::create_directory(path);
                return true;
            } else {
                Logger::info("Folder already exists");
                return false;
            }
        }
    } catch (const std::filesystem::filesystem_error& e) {
        Logger::error("Failed to create folder: {}", e.what());
        return false;
    }
}